<?php
namespace plugin\ky_shouyin\app\controller;

use support\Request;
use support\Response;
use support\Db;

class OrderController extends BaseController
{
    public function index()
    {
        return $this->view('order/index');
    }

    public function list(Request $request)
    {
        // 模拟数据
        $data = [
            'code' => 0,
            'msg' => '',
            'count' => 1,
            'data' => [
                [
                    'id' => 1,
                    'order_no' => 'DD'.date('YmdHis'),
                    'amount' => '100.00',
                    'status' => '已完成',
                    'store_name' => '总店',
                    'created_at' => date('Y-m-d H:i:s')
                ]
            ]
        ];
        return json($data);
    }

    /**
     * 订单详情
     */
    public function detail(Request $request)
    {
        $id = $request->get('id');
        
        // 获取订单信息
        $order = Db::table('ky_order')
            ->leftJoin('ky_member', 'ky_order.member_id', '=', 'ky_member.id')
            ->leftJoin('ky_store', 'ky_order.store_id', '=', 'ky_store.id')
            ->where('ky_order.id', $id)
            ->first([
                'ky_order.*',
                'ky_member.name as member_name',
                'ky_member.phone as member_phone',
                'ky_store.name as store_name'
            ]);
        
        // 获取订单商品
        if ($order) {
            $order->goods = Db::table('ky_order_goods')
                ->leftJoin('ky_goods', 'ky_order_goods.goods_id', '=', 'ky_goods.id')
                ->where('order_id', $id)
                ->get([
                    'ky_order_goods.*',
                    'ky_goods.name as goods_name',
                    'ky_goods.image'
                ]);
        }
        
        return $this->view('order/detail', ['order' => $order]);
    }

    /**
     * 取消订单
     */
    public function cancel(Request $request)
    {
        $id = $request->post('id');
        
        try {
            Db::beginTransaction();
            
            // 获取订单商品
            $orderGoods = Db::table('ky_order_goods')
                ->where('order_id', $id)
                ->get();
            
            // 恢复库存
            foreach ($orderGoods as $goods) {
                Db::table('ky_goods')
                    ->where('id', $goods->goods_id)
                    ->increment('stock', $goods->quantity);
            }
            
            // 更新订单状态
            Db::table('ky_order')
                ->where('id', $id)
                ->update([
                    'status' => 2, // 已取消
                    'updated_at' => date('Y-m-d H:i:s')
                ]);
            
            // 如果有使用积分，恢复积分
            $order = Db::table('ky_order')
                ->where('id', $id)
                ->first();
                
            if ($order->points_used > 0) {
                Db::table('ky_member')
                    ->where('id', $order->member_id)
                    ->increment('points', $order->points_used);
            }
            
            Db::commit();
            return json(['code' => 0, 'msg' => '取消成功']);
        } catch (\Exception $e) {
            Db::rollBack();
            return json(['code' => 1, 'msg' => '取消失败：' . $e->getMessage()]);
        }
    }
} 